

// 包装类型
var num = new Number(123);
console.log(typeof num);

var gavin = new String('gavin');
console.log(typeof gavin);

var flag = new Boolean(true);
console.log(typeof flag);

// 基本类型包装对象
var data1 = 123;
var data2 = new Number(123);

console.log(typeof data1);
console.log(typeof data2);
console.log(Number.parseFloat(data1)); 
console.log("is equal: %s", (data1 === data2)); // false

var name = "gavin";
var name2 = new String("gavin");

console.log(typeof(name));
console.log(typeof(name2));

// 创建对象
// 手动的写出对象的内容
var john = { 

	name: ["John", "Doe"],
	age: 25,
	gender: 'male',
	
	bio: function() {
		return this.name[0] + ' ' + this.name[1] + ' ' + this.age;
	},
	
	greeting: function() {
		return "Hi, I\'m " + this.name[0];
	}

};


// 属性
console.log(john.name);
delete john.age;
john.age = 18;
console.log(john.age);


// 检查对象是否拥有某属性，用in操作符
console.log("name" in john);
// 继承来的属性也可
console.log("toString" in john); 

// 检查属性是否本身拥有，而不是继承得到的
console.log(john.hasOwnProperty("name"));

// 方法
console.log(john.bio());
console.log("%s, age:%d", john.greeting(), john['age']);

// 赋值的是对象的引用
var copy = john;  
copy.name[0] = "gavin"; 

console.log(john.bio());
console.log(copy.bio());

// 通过构造函数来创建类
// 构造函数通常大写开头
function Person(firstName, lastName, age) {
	// 属性
	this.firstName = firstName;
	this.lastName = lastName;
	this.age = age;
	// 方法
	this.bio = function() {
		return this.firstName + ' ' + this.lastName;
	};
	this.greeting = function() {
		return "Hi, i am " + this.firstName + ' ' + this.lastName;
	};
}

var gavin = new Person("gavin", "ge", 27);

console.log(gavin.greeting());

console.log(gavin.constructor === Person.prototype.constructor); // true
console.log(Person.prototype.constructor === Person);   // true


// 演示原型继承
// 原型对象
var Student = {
	name: 'robot',
	height: 1.2,
	run: function() {
		console.log(this.name + " is running ....");
	}
};

var xiaoming = {
	name: '小明'
};

// 在编写JavaScript代码时，不要直接用obj.__proto__去改变对象的原型
xiaoming.__proto__ = Student;  
console.log(xiaoming.name);
console.log(xiaoming.run());

function createStudent(name) {
	// 基于Student原型创建一个新对象
	var s = Object.create(Student);
	s.name = name;
	return s;
}

var xiaozhang = createStudent("小张");
xiaozhang.run();





